package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.HmzInventoryCheckDetailMapper;
import com.ruoyi.system.service.IHmzProductService;
import com.ruoyi.system.service.IHmzProductTypeService;
import com.ruoyi.system.service.IHmzWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HmzInventoryCheckMapper;
import com.ruoyi.system.service.IHmzInventoryCheckService;

/**
 * 用于存储盘点单信息的Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-10
 */
@Service
public class HmzInventoryCheckServiceImpl implements IHmzInventoryCheckService
{
    @Autowired
    private HmzInventoryCheckMapper hmzInventoryCheckMapper;

    @Autowired
    private HmzInventoryCheckDetailMapper hmzInventoryCheckDetailMapper;
    @Autowired
    private IHmzProductService hmzProductService;
    @Autowired
    private IHmzProductTypeService hmzProductTypeService;
    @Autowired
    protected IHmzWarehouseService hmzWarehouseService;

    /**
     * 查询用于存储盘点单信息的
     *
     * @param id 用于存储盘点单信息的主键
     * @return 用于存储盘点单信息的
     */
    @Override
    public HmzInventoryCheck selectHmzInventoryCheckById(Long id)
    {
        HmzInventoryCheck hmzInventoryCheck = hmzInventoryCheckMapper.selectHmzInventoryCheckById(id);
        HmzInventoryCheckDetail hmzInventoryCheckDetail = new HmzInventoryCheckDetail();
        hmzInventoryCheckDetail.setInventoryCheckId(id);
        //查询到明细
        List<HmzInventoryCheckDetail> hmzInventoryCheckDetails = hmzInventoryCheckDetailMapper.selectHmzInventoryCheckDetailList(hmzInventoryCheckDetail);
        if ( hmzInventoryCheckDetails!=null && hmzInventoryCheckDetails.size() > 0){
            List<HmzProduct> arrayList = new ArrayList<>();
            for (HmzInventoryCheckDetail inventoryCheckDetail : hmzInventoryCheckDetails) {
                Long productId = inventoryCheckDetail.getProductId();
                HmzProduct hmzProduct = hmzProductService.selectHmzProductById(productId);
                hmzProduct.setInventoryStatus(inventoryCheckDetail.getInventoryStatus());
                Long productTypeId = hmzProduct.getProductTypeId();
                //产品类别
                HmzProductType hmzProductType = hmzProductTypeService.selectHmzProductTypeById(productTypeId);
                if(StringUtils.isNotNull(hmzProductType)){
                    hmzProduct.setProductTypeIdName(hmzProductType.getProductTypeName());
                }
                arrayList.add(hmzProduct);
            }
//            hmzInventoryCheck.setInfoList(hmzInventoryCheckDetails);
            hmzInventoryCheck.setInfoListS(arrayList);
        }
        return hmzInventoryCheck;
    }

    /**
     * 查询用于存储盘点单信息的列表
     *
     * @param hmzInventoryCheck 用于存储盘点单信息的
     * @return 用于存储盘点单信息的
     */
    @Override
    public List<HmzInventoryCheck> selectHmzInventoryCheckList(HmzInventoryCheck hmzInventoryCheck)
    {
        List<Integer> ids = hmzInventoryCheck.getIds();
        List<HmzInventoryCheck> hmzInventoryChecks = hmzInventoryCheckMapper.selectHmzInventoryCheckList(hmzInventoryCheck);
        if(hmzInventoryChecks !=null && hmzInventoryChecks.size() > 0){
            for (HmzInventoryCheck inventoryCheck : hmzInventoryChecks) {
                Long id = inventoryCheck.getId();
                HmzInventoryCheckDetail hmzInventoryCheckDetail = new HmzInventoryCheckDetail();
                hmzInventoryCheckDetail.setInventoryCheckId(id);
                List<HmzInventoryCheckDetail> hmzInventoryCheckDetails = hmzInventoryCheckDetailMapper.selectHmzInventoryCheckDetailList(hmzInventoryCheckDetail);
                if ( hmzInventoryCheckDetails!=null && hmzInventoryCheckDetails.size() > 0){
                    List<HmzProduct> arrayList = new ArrayList<>();
                    for (HmzInventoryCheckDetail inventoryCheckDetail : hmzInventoryCheckDetails) {
                        Long warehouseId = inventoryCheckDetail.getWarehouseId();
                        HmzWarehouse hmzWarehouse = hmzWarehouseService.selectHmzWarehouseById(warehouseId);
                        if(StringUtils.isNotNull(hmzWarehouse)){
                            inventoryCheckDetail.setWarehouseIdName(hmzWarehouse.getWarehouseName());
                        }
                        Long productId = inventoryCheckDetail.getProductId();
                        HmzProduct hmzProduct = hmzProductService.selectHmzProductById(productId);
                        if(StringUtils.isNotNull(hmzProduct)){
                            hmzProduct.setInventoryStatus(inventoryCheckDetail.getInventoryStatus());
                            inventoryCheckDetail.setProductName(hmzProduct.getProductName());
                            inventoryCheckDetail.setProductNameRfid(hmzProduct.getRfidNumber());
                            Long productTypeId = hmzProduct.getProductTypeId();
                            HmzProductType hmzProductType = hmzProductTypeService.selectHmzProductTypeById(productTypeId);
                            if(StringUtils.isNotNull(hmzProductType)){
                                inventoryCheckDetail.setProductTypeIdName(hmzProductType.getProductTypeName());
                                hmzProduct.setProductTypeIdName(hmzProductType.getProductTypeName());
                            }

                            arrayList.add(hmzProduct);
                        }

                    }
                    inventoryCheck.setInfoListS(arrayList);
                    inventoryCheck.setInfoList(hmzInventoryCheckDetails);
                }
            }
        }
        return hmzInventoryChecks;
    }

    /**
     * 新增用于存储盘点单信息的
     *
     * @param hmzInventoryCheck 用于存储盘点单信息的
     * @return 结果
     */
    @Override
    public int insertHmzInventoryCheck(HmzInventoryCheck hmzInventoryCheck)
    {
        hmzInventoryCheck.setCreateTime(DateUtils.getNowDate());
        int i = hmzInventoryCheckMapper.insertHmzInventoryCheck(hmzInventoryCheck);
        //获取自增id
        Long id = hmzInventoryCheck.getId();
        String inventoryCheckNumber = hmzInventoryCheck.getInventoryCheckNumber();
        List<HmzInventoryCheckDetail> infoList = hmzInventoryCheck.getInfoList();
        if(infoList !=null && infoList.size() > 0){
            for (HmzInventoryCheckDetail hmzInventoryCheckDetail : infoList) {
                hmzInventoryCheckDetail.setInventoryCheckId(id);
                hmzInventoryCheckDetail.setProductId(hmzInventoryCheckDetail.getId());
                hmzInventoryCheckDetail.setInventoryCheckNumber(inventoryCheckNumber);
                hmzInventoryCheckDetailMapper.insertHmzInventoryCheckDetail(hmzInventoryCheckDetail);
            }
        }
        return i;
    }

    /**
     * 修改用于存储盘点单信息的
     *
     * @param hmzInventoryCheck 用于存储盘点单信息的
     * @return 结果
     */
    @Override
    public int updateHmzInventoryCheck(HmzInventoryCheck hmzInventoryCheck)
    {
        hmzInventoryCheck.setUpdateTime(DateUtils.getNowDate());
        return hmzInventoryCheckMapper.updateHmzInventoryCheck(hmzInventoryCheck);
    }

    /**
     * 批量删除用于存储盘点单信息的
     *
     * @param ids 需要删除的用于存储盘点单信息的主键
     * @return 结果
     */
    @Override
    public int deleteHmzInventoryCheckByIds(Long[] ids)
    {
        return hmzInventoryCheckMapper.deleteHmzInventoryCheckByIds(ids);
    }

    /**
     * 删除用于存储盘点单信息的信息
     *
     * @param id 用于存储盘点单信息的主键
     * @return 结果
     */
    @Override
    public int deleteHmzInventoryCheckById(Long id)
    {
        return hmzInventoryCheckMapper.deleteHmzInventoryCheckById(id);
    }

    @Override
    public List<HmzInventoryCheckDetail> selectHmzInventoryCheckByIdNew(List<Integer> ids) {
        List<HmzInventoryCheckDetail> lists =new ArrayList<>();
        for (Integer id : ids) {
            HmzInventoryCheck hmzInventoryCheck = hmzInventoryCheckMapper.selectHmzInventoryCheckById(Long.valueOf(id));
            HmzInventoryCheckDetail hmzInventoryCheckDetail = new HmzInventoryCheckDetail();
            hmzInventoryCheckDetail.setInventoryCheckId(Long.valueOf(id));
            //查询到明细
            List<HmzInventoryCheckDetail> hmzInventoryCheckDetails = hmzInventoryCheckDetailMapper.selectHmzInventoryCheckDetailList(hmzInventoryCheckDetail);
            if ( hmzInventoryCheckDetails!=null && hmzInventoryCheckDetails.size() > 0){
//                List<HmzProduct> arrayList = new ArrayList<>();
                for (HmzInventoryCheckDetail inventoryCheckDetail : hmzInventoryCheckDetails) {
                    inventoryCheckDetail.setInventoryCheckNumber(hmzInventoryCheck.getInventoryCheckNumber());
                    inventoryCheckDetail.setTotalQuantity(inventoryCheckDetail.getTotalQuantity());
                    inventoryCheckDetail.setCheckedQuantity(inventoryCheckDetail.getCheckedQuantity());
                    inventoryCheckDetail.setUncheckedQuantity(inventoryCheckDetail.getUncheckedQuantity());
                    Long productId = inventoryCheckDetail.getProductId();
                    HmzProduct hmzProduct = hmzProductService.selectHmzProductById(productId);
                    if(StringUtils.isNotNull(hmzProduct)){
                        inventoryCheckDetail.setProductName(hmzProduct.getProductName());
                    }
                    hmzProduct.setInventoryStatus(inventoryCheckDetail.getInventoryStatus());
                    Long productTypeId = hmzProduct.getProductTypeId();
                    //产品类别
                    HmzProductType hmzProductType = hmzProductTypeService.selectHmzProductTypeById(productTypeId);
                    if(StringUtils.isNotNull(hmzProductType)){
                        inventoryCheckDetail.setProductTypeIdName(hmzProductType.getProductTypeName());
//                        hmzProduct.setProductTypeIdName(hmzProductType.getProductTypeName());
                    }
                    lists.add(inventoryCheckDetail);
//                    arrayList.add(hmzProduct);
                }
//            hmzInventoryCheck.setInfoList(hmzInventoryCheckDetails);
//                hmzInventoryCheck.setInfoListS(arrayList);
            }
        }

        return lists;
    }
}
